//
//  RoomDataProcess.m
//  QRMaster
//  房间数据的处理，是针对房间权限和成员时间周期分配
//  Created by myApple on 16/5/16.
//  Copyright © 2016年 myApple. All rights reserved.
//

#import "RoomDataProcess.h"
#import "ItemModel.h"
#import "AllocateRoomModel.h"
#import "setTimeModel.h"
#import "RoomDataProcess.h"
#import "AssignRoomTimeModel.h"
#import "AssignFloorTimeModel.h"
#import "AssignBuildTimeModel.h"

@interface RoomDataProcess()
{
    NSMutableArray * bulidingArray;
}

@end

@implementation RoomDataProcess

-(NSMutableArray *)dataProcess:(NSArray *)data
{
    bulidingArray = [[NSMutableArray alloc]init];
    NSMutableArray * floorArray = [[NSMutableArray alloc]init];
    NSMutableArray * roomArray = [[NSMutableArray alloc]init];
    for (NSDictionary * dict in data) {
        ItemModel * bmodel = [[ItemModel alloc]init];
        bmodel.itemId = [dict objectForKey:@"buildingid"];
        bmodel.itemNo = [dict objectForKey:@"buildingno"];
        bmodel.itemName = [dict objectForKey:@"buildingname"];
        bmodel.itemChildrenArray = [dict objectForKey:@"floors"];
        bmodel.nodeLevel = 1;
        bmodel.nodeType = NodeTypeGroup;
        bmodel.isClick = NO;
        bmodel.nodeId = [dict objectForKey:@"buildingid"];
        if (bmodel.itemChildrenArray) {
            [floorArray removeAllObjects];
            for (NSDictionary * fdict in bmodel.itemChildrenArray) {
                ItemModel * fmodel = [[ItemModel alloc]init];
                fmodel.itemId = [fdict objectForKey:@"floorid"];
                fmodel.itemName = [fdict objectForKey:@"floorname"];
                fmodel.itemNo = [fdict objectForKey:@"floorno"];
                fmodel.itemChildrenArray = [fdict objectForKey:@"rooms"];
                fmodel.nodeLevel = 2;
                fmodel.isClick = NO;
                fmodel.nodeType = NodeTypeGroup;
                fmodel.nodeId = [dict objectForKey:@"floorid"];
                if (fmodel.itemChildrenArray) {
                    [roomArray removeAllObjects];
                    for (NSDictionary * rdict in fmodel.itemChildrenArray) {
                        AllocateRoomModel * rmodel = [[AllocateRoomModel alloc]init];
                        rmodel.roomid = [rdict objectForKey:@"roomid"];
                        rmodel.roomName = [rdict objectForKey:@"roomname"];
                        rmodel.roomno = [rdict objectForKey:@"roomno"];
                        rmodel.isSelect = [[rdict objectForKey:@"isSelected"]intValue];//是否被分配的房间
                        rmodel.nodeLevel = 3;
                        rmodel.nodeType = NodeTypeItem;
                        rmodel.nodeId = [dict objectForKey:@"roomid"];
                        if (rmodel.isSelect == 1) {
                            NSArray * getRoomArray = [rdict objectForKey:@"timeInfo"];
                            if ([rdict objectForKey:@"timeInfo"] != [NSNull null] ) {
                                if (getRoomArray.count > 0) {
                                    NSMutableArray * roomTimeArray = [[NSMutableArray alloc]init];
                                    for (NSDictionary * tDict in getRoomArray) {
                                        setTimeModel * model = [[setTimeModel alloc]init];
                                        model.beginTime = [tDict objectForKey:@"startTime"];
                                        model.endTime = [tDict objectForKey:@"endTime"];
                                        model.weekInfo = [tDict objectForKey:@"cycleString"];
                                        model.timeId = [tDict objectForKey:@"timeId"];
                                        switch ([model.timeId integerValue]) {
                                            case 47:
                                                model.timeImage = [UIImage imageNamed:@"roomTime1"];
                                                break;
                                            case 48:
                                                  model.timeImage = [UIImage imageNamed:@"roomTime2"];
                                                break;
                                            case 49:
                                                  model.timeImage = [UIImage imageNamed:@"roomTime3"];
                                                break;

                                            default:
                                                break;
                                        }
                                        [roomTimeArray addObject:model];
                                    }
                                    rmodel.timeArray = roomTimeArray;
                                    fmodel.itemSelect ++;
                                }
                            }
                            rmodel.isSelect = 0;
                            fmodel.itemSum ++;
                            [roomArray addObject:rmodel];
                        }
                    }
                    if (roomArray.count > 0) {
                         bmodel.itemSum ++;
                        if (fmodel.itemSelect > 0) {
                             bmodel.itemSelect ++;
                        }
                        fmodel.itemChildrenArray = [roomArray copy];
                        [floorArray addObject:fmodel];
                    }
                }
                
            }
            if (floorArray.count > 0) {
                bmodel.itemChildrenArray = [floorArray copy];
                [bulidingArray addObject:bmodel];
            }
        }
    }
    return bulidingArray;
}

#pragma mark -------获取新模型的房间
-(NSMutableArray *)getRoomDataAllocate
{
    NSMutableArray * roomArray = [NSMutableArray array];
    for (ItemModel * bModel in bulidingArray) {
        for (ItemModel * fModel in bModel.itemChildrenArray) {
            for (AllocateRoomModel * rModel in fModel.itemChildrenArray) {
                AssignRoomTimeModel * rTModel = [[AssignRoomTimeModel alloc]init];
                rTModel.roomId = rModel.roomid;
                rTModel.roomNo  = rModel.roomno;
                rTModel.roomName = rModel.roomName;
                rTModel.bulidingName = bModel.itemName;
                rTModel.floorName = fModel.itemName;
                rTModel.timeArray = rModel.timeArray;
                [roomArray addObject:rTModel];
            }
        }
    }
    return roomArray;
}

#pragma mark ------获取新模型的楼层
-(NSMutableArray *)getFloorDataAllocate
{
    NSMutableArray * floorArray = [NSMutableArray array];
    for (ItemModel * bModel in bulidingArray) {
        for (ItemModel * fModel in bModel.itemChildrenArray) {
            AssignFloorTimeModel * model = [[AssignFloorTimeModel alloc]init];
            model.floorId = fModel.itemId;
            model.floorName = fModel.itemName;
            model.bulidingName = bModel.itemName;
            model.selectRoom = fModel.itemSelect;
            model.totalRoom = fModel.itemSum;
            [floorArray addObject:model];
        }
    }
    return floorArray;
}

#pragma mark ------获取新模型的楼栋
-(NSMutableArray *)getBuildDataAllocate
{
    NSMutableArray * buildArray = [NSMutableArray array];
    for (ItemModel * bModel in bulidingArray) {
        AssignBuildTimeModel * model = [[AssignBuildTimeModel alloc]init];
        model.buildingid = bModel.itemId;
        model.buildingno = bModel.itemNo;
        model.bulidingName = bModel.itemName;
        model.sumRoom = bModel.itemSum;
        model.floorArray = bModel.itemChildrenArray;
        [buildArray addObject:model];
    }
    return buildArray;
}

@end
